package com.neteast.bustruck.db

import android.content.Context
import android.util.Log
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import androidx.sqlite.db.SupportSQLiteDatabase
import com.neteast.bustruck.db.dao.LocalSaveInfoDao
import com.neteast.bustruck.db.dao.TruckLoadDao
import com.neteast.bustruck.db.dao.TruckLogDao
import com.neteast.bustruck.db.dao.UserDao
import com.neteast.bustruck.db.entiys.LocalSaveInfo
import com.neteast.bustruck.db.entiys.TmTruckLoadForm
import com.neteast.bustruck.db.entiys.TruckLogInfo
import com.neteast.bustruck.db.entiys.User


@Database(entities = [User::class,LocalSaveInfo::class, TmTruckLoadForm::class, TruckLogInfo::class],version = 1)
abstract  class AppDataBase :RoomDatabase() {

    abstract fun userDao(): UserDao

    abstract fun infoDao():LocalSaveInfoDao

    abstract fun truckLoadDao():TruckLoadDao

    abstract fun  truckLogDao():TruckLogDao

    companion object{
        private val TAG: String? = AppDataBase::class.simpleName

        @Volatile
        private var INSTANCE: AppDataBase? = null

        fun getInstance(context: Context): AppDataBase {
                synchronized(this){
                    var instance = INSTANCE
                    if(instance==null){
                        instance = Room.databaseBuilder(context, AppDataBase::class.java, "bus_.db")
                            .fallbackToDestructiveMigration()
                            //是否允许在主线程进行查询
                            .allowMainThreadQueries()
                            .fallbackToDestructiveMigration()
                            .addCallback(object : Callback() {
                                override fun onCreate(db: SupportSQLiteDatabase) {
                                    super.onCreate(db)
                                    Log.e(TAG, "onCreate db_name is=" + db.path)
                                }
                            })
                            .build()
                            INSTANCE = instance
                    }
                    return instance
                }
        }

    }

}